/*!
 * Register Verify
 * Version: 1.0.0
 * Modify: By Nian.Liu
 * Date: 08-22-2013 18:11:48 (GMT Time)
 */
var tagEleTip = {
	account : $('#account'),//账号
	password : $('#password'),//密码
	rePassword : $('#rePassword'),//确认密码
	refferee : $('#refferee'),//推荐人
	captcha : $('#captcha'),//验证码
	errorMsg : $('#errorMsg'),//信息提示
	agreement : $('#agreement'),
};
//js去除空格函数
function LTrim(str)
{
    var i;
    for(i=0;i<str.length;i++)
    {
        if(str.charAt(i)!=" "&&str.charAt(i)!=" ")break;
    }
    str=str.substring(i,str.length);
    return str;
}
function RTrim(str)
{
    var i;
    for(i=str.length-1;i>=0;i--)
    {
        if(str.charAt(i)!=" "&&str.charAt(i)!=" ")break;
    }
    str=str.substring(0,i+1);
    return str;
}
function Trim(str)
{
    return LTrim(RTrim(str));
}

function getLength(str){
	return str.replace(/([^\x00-\xFF])/g,'**').length;; 
}
// 检测用户名输入是否以数字开头
function checkNameBeginByNum(str){
	//不限定手机号码输入
	var regMobile = /^(13[0-9]|15[0-9]|18[0-9])\d{8}$/;
	if(regMobile.test(str)){
    	return true;
    }
	
    var number = str.substring(0,1);
    var reg = /^\d+$/;
    if (!number.match(reg)){
      return true;
    }else{    
      return false;        
    }    
}
// 判断昵称是否重名
function ajaxNkname(str) { 
	var rs;
    $.ajaxSetup({
        async : false
    });
    var param = {};
	param["paramMap.email"] = "";
	param["paramMap.userName"] = str;    		     	  
	$.post("ajaxCheckRegister.do",param,function(data){
		rs = data;
	});
	return rs;
}
// 检测用户名信息
var rsName = false;
var bName = "";
function chekUsNamefor() {
    var nkName = tagEleTip.account.val();
    var myReg = /^[_A-Za-z0-9\u4e00-\u9fa5]+$/;    
    if (bName != nkName) {
        rsName = false;
    }
    if (rsName) {
        return true;
    }
    bName = nkName;
    if ($.trim(nkName) == "" || nkName == null || nkName == '请输入用户名') {
        tagEleTip.errorMsg.html("用户名不能为空").show();
	    return false;
    } else if(!checkNameBeginByNum(nkName)){
        tagEleTip.errorMsg.html("不能数字开头").show();
        return false;
    }else if(getLength(nkName)<4){
        tagEleTip.errorMsg.html("昵称太短了").show();
        return false;
    }else if(getLength(nkName)>18){
        tagEleTip.errorMsg.html("昵称过长").show();
        return false;
    }else if(!myReg.test(nkName)){
        tagEleTip.errorMsg.html("昵称只能为字母、数字或中文").show();
        return false;
    }else {
        tagEleTip.errorMsg.html("").show();
    }
    // FIXME add by hejiangfeng 暂时这样去简单校验
    var rs = ajaxNkname(nkName);
	if (rs == 3) {
		tagEleTip.errorMsg.html("ID已被注册").show();
		return false;
	} else {
		tagEleTip.errorMsg.html("").show();
		//tagEleTip.signNameTip.append("<i class='icons green-proper'></i>");
	}
    rsName = true;
    return true;
}
// 验证密码
function chekPwdfor() {
	var re = false;
	var pwd = tagEleTip.password.val();
	tagEleTip.errorMsg.html("").show();;
	if ($.trim(pwd) == "" || pwd == null ) {
		tagEleTip.errorMsg.html("密码不能为空").show();
		return re;
	}else if(pwd.length<6||pwd.length>16){
		tagEleTip.errorMsg.html("长度:6-16位").show();
		return re;
	}
	else {
		tagEleTip.errorMsg.html("").show();
		return true;
	}
}
// 验证重新输入的密码
function checkPWDui() {  // 密码确认
	var re = false;
	var PWDui = tagEleTip.rePassword.val();
	var PWD = tagEleTip.password.val();
	if ($.trim(PWD) == "" || PWD == null) {
		tagEleTip.errorMsg.html("请先输入密码").show();
		return re;
	} else if (PWDui == "" || PWDui == null) {
		tagEleTip.errorMsg.html("请再次输入").show();
		return re;
	} else if (PWD != PWDui) {
		tagEleTip.errorMsg.html("密码不一致").show();
		return re;
	} else if (PWD == PWDui) {
		tagEleTip.errorMsg.html("").show();
		re = true;
		return re;
	} 
}
//刷新验证码
function refresh(str) {
	var timenow = new Date();
	$("#imgCaptcha").attr("src","I4008lOve893gOfUll983/imageCode.do?pageId="+str+"&d="+ timenow);
	//$("#imgCaptcha").attr("src", environment.basePath + "/views/user/verifyCode.jsp?r=" + new Date().getTime());
}
//邀请人校验
function checkRefferee(){
	var refferee = $('#refferee').val();
	if(refferee!=null && refferee!= ""){
		$.post("queryValidRecommer.do",{refferee:refferee},function(data){
			if(data == 1){
				tagEleTip.errorMsg.html("经纪人不存在").show();
				switchCode('wchatRegister');
				return false;
			}else{
				tagEleTip.errorMsg.html("").show();
			}
		});
	}
	return true;
}
// 验证码校验
function chekVerifyCode(){ // 验证码输入提示
	var vCode = tagEleTip.captcha.val();
	if($.trim(vCode) == ""|| vCode == null){
		tagEleTip.errorMsg.html("请输入验证码").show();
		return false;		
	}else{
		tagEleTip.errorMsg.html("").show();
	}
	return true;
}
// 下一步按钮提交操作
function register() {
	$('#signSubmit').attr('href','javascript:;');
	var t1 = chekUsNamefor();
	if(! t1) return false;
	var t2 = chekPwdfor();
	if(! t2) return false;
	var t3 = checkPWDui();
	if(! t3) return false;
	var t4 = chekVerifyCode();
	if(! t4) return false;

	if(tagEleTip.agreement.prop('checked')!=true){
		tagEleTip.errorMsg.html("请同意协议").show();
		return;
	}else{
		tagEleTip.errorMsg.html("").show();
	}
	var param = {};
    param["paramMap.pageId"] = "wchatRegister";
    param["paramMap.userName"] = tagEleTip.account.val();
    param["paramMap.password"] = tagEleTip.password.val();
    param["paramMap.confirmPassword"] = tagEleTip.rePassword.val();
    param["paramMap.refferee"] = tagEleTip.refferee.val();
    param["paramMap.code"] = tagEleTip.captcha.val();
	var rs = null;
	$(".regist :input").attr("disabled", true);
	//FIXME 暂时就这样简单校验
	$.post("wchatRegisterFir.do",param,function(data){
		if(data.error=='0'){
			 $(".regist :input").removeAttr('disabled');
			 tagEleTip.errorMsg.html("ID已被注册").show();
			 refresh('wchatRegister');
	    }else if(data.error=='1'){
			 $(".regist :input").removeAttr('disabled');
			 tagEleTip.errorMsg.html("用户名长度为2-20个字符").show();
			 switchCode('wchatRegister');
	    }else if(data.error=='2'){
			 $(".regist :input").removeAttr('disabled');
			 tagEleTip.errorMsg.html("用户名不能为空").show();
			 switchCode('wchatRegister');
	    }else if(data.error=='3'){
			 $(".regist :input").removeAttr('disabled');
			 tagEleTip.errorMsg.html("用户名不能含有特殊字符").show();
			 switchCode('wchatRegister');
	    }else if(data.error=='4'){
			 $(".regist :input").removeAttr('disabled');
			 tagEleTip.errorMsg.html("用户名不能以下划线开头").show();
			 switchCode('wchatRegister');
	    }else if(data.error=='5'){
			 $(".regist :input").removeAttr('disabled');
			 tagEleTip.errorMsg.html("密码不能为空").show();
			 switchCode('wchatRegister');
	    }else if(data.error=='6'){
			 $(".regist :input").removeAttr('disabled');
			 tagEleTip.errorMsg.html("确认密码不能为空").show();
			 switchCode('wchatRegister');
	    }else if(data.error=='7'){
			 $(".regist :input").removeAttr('disabled');
			 tagEleTip.errorMsg.html("两次输入密码不一致").show();
			 switchCode('wchatRegister');
	    }else if(data.error=='12'){
			 $(".regist :input").removeAttr('disabled');
			 tagEleTip.errorMsg.html("注册异常").show();
			 switchCode('wchatRegister');
	    }else if(data.refferee=='1'){
			 $(".regist :input").removeAttr('disabled');
			 tagEleTip.errorMsg.html("邀请人不存在").show();
			 switchCode('wchatRegister');
	    }else if(data._checkCode=='1'){
	    	 $(".regist :input").removeAttr('disabled');
			 tagEleTip.errorMsg.html("验证码错误").show();
			 switchCode('wchatRegister');
        }else {
        	 var urlStr = "wchatRegisterSec.do";
        	 if(data.mobileLast != null){
        		 urlStr  = urlStr + "?mobileLast=" + data.mobileLast;
        	 }
        	 window.location.href=urlStr;
        }
     });
}